<template>
  <view class="add-goods">
    <public-header :isBack="true" bgColor="#28322B" textColor="#fff" :title="pageTitle"></public-header>
    <u-loading-page :loading="true" v-if="loading"></u-loading-page>
    <bx-form :serviceName="serviceName" :pk="pk" :pkCol="pkCol" v-if="serviceName" :type="type" :gridCfg="gridCfg"
      @cancel="back" @on-submit="back" @fields-loaded="hideLoading">
    </bx-form>
  </view>
</template>

<script>
  const gridCfg = [{
      // 四行五列
      "range": "4,5",
      "columnGap": "0rem",
      "rowGap": "0rem",
      "cols": [{
          "label": false,
          "limit": 1,
          "col": "goods_image",
          // row-start col-start row-end col-end
          "area": "1,1,5,1"
        },
        {
          "col": "goods_barcode",
          // 'grid-row-start':'span 1',
          'grid-column-start': 'span 2'
          // "area": "1,2,1,4"
        },
        {
          "col": "goods_classify",
          // 'grid-row-start':'span 1',
          'grid-column-start': 'span 2'
          // "area": "1,4,1,6"
        },
        {
          "col": "goods_name",
          // 'grid-row-start':'span 1',
          'grid-column-start': 'span 2'
          // "area": "2,2,2,4"
        },
        {
          "col": "initial_stock",
          // "area": "2,4,2,6"
          'grid-column-start': 'span 2'
        },
        {
          "col": "sale_price",
          // "area": "3,2,3,4"
          'grid-column-start': 'span 2'
        },
        {
          "col": "purchase_price",
          // "area": "3,4,3,6"
          'grid-column-start': 'span 2'
        },
        {
          "col": "goods_unit",
          'grid-column-start': 'span 2'
          // "area": "4,2,4,4"
        },
        {
          "col": "supplier",
          // "area": "4,4,4,6",
          'grid-column-start': 'span 2'
        }
      ]
    },
    {
      // 五行两列
      "range": "0,2",
      "columnGap": "0rem",
      "rowGap": "0rem",
      "bg": '#f5f5f5',
      "cols": [{
          "col": "cashier_setup",
          "area": "1,1,1,1"
        },
        {
          "col": "allow_member_price",
          "area": "1,2,1,2"
        },
        {
          "foreign_key": {
            "id": 316,
            "constraint_name": "e5c6244c28c2ab9d5b700f9fe01c762c",
            "table_name": "bxretail_goods_member",
            "column_name": "goods_no",
            "referenced_table_name": "bxretail_goods_info",
            "referenced_column_name": "goods_no",
            "keydispcol": "goods_name",
            "foreign_key_type": "主子表",
            "update_rule": "CASCADE",
            "delete_rule": "CASCADE",
            "create_to_physical": "否",
            "actual_physical_state": null,
            "show_ui_child_table": "是",
            "show_child_list_expr": "{{mainData.allow_member_price=='允许'}}",
            "show_ui_seq": 100,
            "show_ui_model": "collapse",
            "view_model": "list",
            "card_no": null,
            "child_table_readonly": "否",
            "section_name": "会员价",
            "show_ui_grandson_table": "否",
            "section_grandson_name": null,
            "more_config": "{\n  \"type\":\"field\",\n  \"colName\":\"allow_member_price\",\n  \"collapse\":false,\n  \"position\":\"append\"\n}",
            "depend_fk": null,
            "save_fmt": "fk",
            "save_col": null,
            "seq_col": null,
            "model_type": "1",
            "order_type": "降序",
            "select_type": "下拉选择",
            "moreConfig": {
              "type": "field",
              "colName": "allow_member_price",
              "collapse": false,
              "position": "append"
            }
          },
          "area": "2,2,6,2",
          // "grid-column-start": "span 1",
          // "grid-row-start": "span 4",
          "col": "xxx",
          "type": "addchildlist",
          "use_type": "addchildlist",
          "show_child_list_expr": "{{mainData.allow_member_price=='允许'}}",
          "label": "会员价",
          "keydispcol": "goods_name",
          "constraint_name": "f07b5d55dbfd630e74a55d6400bc745a",
          // "area": "2,1,2,1"
          "referenced_column_name": "goods_no",
          "column_name": "goods_no",
          service_name: "srvretail_goods_member_select",
          section_name: "会员价",
          select_type: "下拉选择",
          view_model: 'list',
          showDelete: false,
          disabled: true,
          "inlineEditCols": "member_discount_amount", //可行内编辑字段
          defaultData: [{
            "discount_no": "ZK202210260019",
            "member_level_no": "DJ202210260035",
            "member_level_name": "普通会员",
            "level": "1",
            "discount_rate": 100,
            "_isMemoryData": true,
            "_dirtyFlags": "add"
          }, {
            "discount_no": "ZK202210260020",
            "member_level_no": "DJ202210260036",
            "member_level_name": "银卡等级",
            "level": "2",
            "discount_rate": 95,
            "_isMemoryData": true,
            "_dirtyFlags": "add"
          }, {
            "discount_no": "ZK202210260021",
            "member_level_no": "DJ202210260037",
            "member_level_name": "金卡等级",
            "level": "3",
            "discount_rate": 90,
            "_isMemoryData": true,
            "_dirtyFlags": "add"
          }]
        },
        {
          "col": "sale_state",
          // "area": "2,1,2,1"
        },
        {
          "col": "xxx",
          // "area": "2,2,2,2"
        },
        {
          "col": "valuation_mode",
          // "area": "3,1,3,1"
        },
        {
          "col": "xxx",
          // "area": "3,2,3,2"
        },
        {
          "col": "goods_type",
          // "area": "4,1,4,1"
        },
        {
          // "col": "xxx",
          foreign_key: {
            "id": 212,
            "constraint_name": "f07b5d55dbfd630e74a55d6400bc745a",
            "table_name": "bxretail_package_detail",
            "column_name": "goods_no",
            "referenced_table_name": "bxretail_goods_info",
            "referenced_column_name": "goods_no",
            "keydispcol": "goods_name",
            "foreign_key_type": "主子表",
            "update_rule": "CASCADE",
            "delete_rule": "CASCADE",
            "create_to_physical": "否",
            "actual_physical_state": null,
            "show_ui_child_table": "是",
            "show_child_list_expr": "{{mainData.goods_type=='套餐'}}",
            "show_ui_seq": 100,
            "show_ui_model": "collapse",
            "view_model": "list",
            "card_no": null,
            "child_table_readonly": "否",
            "section_name": "套餐明细",
            "show_ui_grandson_table": "否",
            "section_grandson_name": null,
            "more_config": "{\n  \"type\":\"field\",\n  \"colName\":\"goods_type\",\n  \"collapse\":false,\n  \"position\":\"append\"\n}",
            "depend_fk": null,
            "save_fmt": "fk",
            "save_col": null,
            "seq_col": null,
            "model_type": "1",
            "order_type": "降序",
            "select_type": "下拉选择",
            "moreConfig": {
              "type": "field",
              "colName": "goods_type",
              "collapse": false,
              "position": "append"
            }
          },
          "grid-column-start": "span 1",
          "grid-row-start": "span 4",
          "type": "addchildlist",
          use_type: "addchildlist",
          "show_child_list_expr": "{{mainData.goods_type=='套餐'}}",
          "label": "套餐明细",
          "keydispcol": "goods_name",
          "constraint_name": "f07b5d55dbfd630e74a55d6400bc745a",
          // "area": "2,1,2,1"
          "referenced_column_name": "goods_no",
          "column_name": "goods_no",
          service_name: 'srvretail_package_detail_select',
          section_name: "套餐明细",
          select_type: "下拉选择",
          view_model: 'list',
        },
        {
          "col": "xxx",
          // "area": "4,2,4,2"
        },
        {
          "col": "allow_min_sales_price",
          // "area": "5,1,5,1"
        },
        {
          "col": "xxx",
          // "area": "5,2,5,2"
        }
      ]
    },
    {
      // 四行三列
      "range": "4,3",
      "columnGap": "0rem",
      "rowGap": "0rem",
      "subTitle": "完善商品拓展信息，有助于精确的经营管理",
      "switchDisplay": true,
      "display": false,
      "cols": [{
          "col": "scribing_price",
          "area": "1,1,1,1"
        },
        {
          "col": "minimum_price",
          "area": "1,2,1,2"
        },
        {
          "col": "specifications",
          "area": "1,3,1,3"
        },
        {
          "col": "goods_label",
          "area": "2,1,2,1"
        },
        {
          "col": "brand",
          "area": "2,2,2,2"
        },
        {
          "col": "goods_introduce",
          "area": "3,1,3,3"
        },
        {
          "col": "mnemonic_code",
          "area": "3,3,3,3"
        },
        {
          "col": "quality_guarantee_num",
          "area": "4,1,4,1"
        },
        {
          "col": "place_of_origin",
          "area": "4,2,4,2"
        },
        {
          "col": "manufacture_date",
          "area": "4,3,4,3"
        }
      ]
    }
  ]
  import lockscreen from '@/common/mixins/lockscreen.js'
  export default {
    mixins: [lockscreen],
    data() {
      return {
        loading: true,
        pk: "",
        pkCol: 'id',
        type: "update",
        gridCfg: [],
        // gridCfg: Object.freeze(gridCfg),
        serviceName: "srvretail_goods_info_update"
      }
    },
    computed: {
      pageTitle() {
        let title = {
          'add': '新增商品',
          'update': '编辑商品'
        }
        return title[this.type]
      }
    },
    methods: {
      hideLoading() {
        this.loading = false
      },
      back() {
        uni.navigateBack()
      },
      back2cash() {
        uni.redirectTo({
          url: '/pages/main/cash/cash'
        })
      },
    },
    onLoad(option) {

      if (option.pk || option.id) {
        this.pk = option.pk || option.id;
      }

      if (option.pkCol) {
        this.pkCol = option.pkCol;
      }

      if (option.type) {
        this.type = option.type
      }

      if (option.serviceName) {
        this.serviceName = option.serviceName
      }
    }
  }
</script>

<style lang="scss" scoped>
  .add-goods {
    width: 100vw;
    height: 100vh;
    z-index: 10;
  }

  ::v-deep .form-wrapper {
    .bx-form {
      min-height: calc(100vh - 4rem);
      display: flex;
      flex-direction: column;

      .form-item-content {
        height: 100%;
        padding-right: 0;
      }

      .button-box {
        flex: 1;
        align-items: flex-end;
      }
    }
  }
</style>
